//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using CommunityServer.RssCtrl.Components;
using ResourceControl=CommunityServer.ControlPanel.Controls.ResourceControl;

namespace CommunityServer.ControlPanel.Tools
{
	/// <summary>
	/// Summary description for ManageIPAddresses.
	/// </summary>
	public class RssCtrlsPage : BaseToolsPage
	{

		#region Members

		protected Label StatusMessage;
        protected Repeater Repeater_RssCtrls;
		protected MPContent DescriptionRegion;
		protected MPContent TaskRegion;
		protected MPContainer MPContainer;
		protected StatusMessage Status;
		protected ResourceControl RegionTitle;
        protected LinkButton PurgeFeedsBtn, DeleteUnusedBtn, ClearCacheBtn;

		#endregion

		override protected void OnInit(EventArgs e)
		{
			Load += new EventHandler(Page_Load);
            Repeater_RssCtrls.ItemDataBound += new RepeaterItemEventHandler(Repeater_RssCtrls_ItemDataBound);
            PurgeFeedsBtn.Click += new EventHandler(PurgeFeeds_Click);
            DeleteUnusedBtn.Click += new EventHandler(DeleteUnused_Click);
            ClearCacheBtn.Click += new EventHandler(ClearCache_Click);
            base.OnInit(e);
		}

        void Repeater_RssCtrls_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            switch (e.Item.ItemType)
            {
                case ListItemType.Item:
                case ListItemType.AlternatingItem:
                case ListItemType.SelectedItem:

                    Literal ctrlTitle = e.Item.FindControl("Title") as Literal;
                    Literal Name = e.Item.FindControl("Name") as Literal;

                    RssCtrl.RssCtrl ctrl = e.Item.DataItem as RssCtrl.RssCtrl;
                    ctrlTitle.Text = ctrl.Title;
                    Name.Text = ctrl.Name;

                    HyperLink btn = e.Item.FindControl("ManageButton") as HyperLink;
                    btn.NavigateUrl = string.Format("RssCtrlManage.aspx?ctrlID={0}", ctrl.CtrlID.ToString());
                    break;
            }
        }

		private void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack) 
			{
				Bind();
			}
		}

		private void Bind()
		{
            Repeater_RssCtrls.DataSource = RssCtrls.GetAll();
            Repeater_RssCtrls.DataBind();
		}

        protected void PurgeFeeds_Click(object sender, EventArgs e)
        {
            RssCtrls.Purge();
            ClearCache();
        }

        protected void DeleteUnused_Click(object sender, EventArgs e)
        {
           RssCtrls.Purge(true);
            ClearCache();
        }

        protected void ClearCache_Click(object sender, EventArgs e)
        {
            ClearCache();
        }

        private void ClearCache()
        {
            foreach (RssCtrl.RssCtrl ctrl in RssCtrls.GetAll())
            {
                string cacheKey = ctrl.Name + ctrl.ToString();
                CSCache.Remove(cacheKey); //Blow out the cache to force a refresh
            }
        }



		#region Events

		#endregion

	}
}
